package com.bo.gather.rule;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.bo.gather.bean.GatherBean;
import com.bo.gather.bean.Lengthinfo;
import com.bo.gather.bean.MustIncludeInfo;
import com.bo.gather.bean.RemoveInfo;
import com.bo.gather.bean.ReplaceInfo;

public class BaseRule {
	public static final Map<String,GatherBean> gatherRuleMap=new HashMap<String,GatherBean>();
	
	public static final String gathertitle="gather.title";
	public static final String gathercode="gather.code";
	public static final String gatherparenturl="gather.parent.url";
	public static final String gatherparenthtml="gather.parent.html";
	public static final String gatherparenthtmlrule="gather.parent.html_rule";
	public static final String gatherparentonlyid="gather.parent.only.id";
	
	public static final String gatherchildurl="gather.child.url";
	public static final String gatherchildhtml="gather.child.html";
	public static final String gatherchildhtmlrule="gather.child.html_rule";


	public static final String weibotitle="weibo.title";
	public static final String weibotitlerule="weibo.title.rule";
	
	public static final String weibocontent="weibo.content";
	public static final String weibocontentrule="weibo.content.rule";
	

	public static final String weibourl="weibo.url";
	public static final String weiboimage="weibo.image";
	public static final String weibouser="weibo.user";
	public static final String weibouserrule="weibo.user.rule";
	public static final String weiborandom="weibo.random";
	public static final String weiborandomall="weibo.random.all";
	
	public static final String weiboswitch="weibo.switch";
	
	/**
	 * 计算替换内容
	 * @param parentstr
	 * @param gatherbean
	 */
	public ReplaceInfo calculateReplace(String s){
		//替换
		int replaceindex=s.indexOf("replace:");
		if(replaceindex!=-1){
			String []replacearray=s.substring("replace:".length(), s.length()).split("◎");
			ReplaceInfo replaceInfo=new ReplaceInfo(replacearray[0],replacearray.length>1?replacearray[1]:null);
//			if(contentbean.getReplaceList()==null){
//				List<ReplaceInfo> replacelist=new ArrayList<ReplaceInfo>();
//				replacelist.add(replaceInfo);
//				contentbean.setReplaceList(replacelist);
//			}else{
//				contentbean.getReplaceList().add(replaceInfo);
//			}
//			continue;
			return replaceInfo;
		}
		return null;
	}
	/**
	 * 计算删除
	 * @param s
	 * @return
	 */
	public RemoveInfo calculateRemove(String s){
		//删除操作
		int removeindex=s.indexOf("remove:");
		if(removeindex!=-1){
			String []replacearray=s.substring("remove:".length(), s.length()).split("◎");
			RemoveInfo removeInfo=new RemoveInfo(replacearray[0],replacearray.length>1?replacearray[1]:null);
//			if(contentbean.getRemoveList()==null){
//				List<RemoveInfo> removelist=new ArrayList<RemoveInfo>();
//				removelist.add(removeInfo);
//				contentbean.setRemoveList(removelist);
//			}else{
//				contentbean.getRemoveList().add(removeInfo);
//			}
//			continue;
			return removeInfo;
		}
		return null;
	}
	/**
	 * 计算必须包含
	 * @param s
	 * @return
	 */
	public MustIncludeInfo calculateMustInclude(String s){
		int mustindex=s.indexOf("mustinclude:");
		if(mustindex!=-1){
			String []replacearray=s.substring("mustinclude:".length(), s.length()).split("◎");
			MustIncludeInfo mustIncludeInfo=new MustIncludeInfo(replacearray[0],replacearray.length>1?replacearray[1]:null);

			return mustIncludeInfo;
		}
		return null;
	}
	
	/**
	 * 计算长度
	 * {length:>15;clean:[0-9]clean:[a-y]clean:[A-Y]clean:\\pP|\\pSclean:" "@@delete}
	 * @param s
	 * @return
	 */
	public Lengthinfo calculateLength(String s){
		//验证长度
		int lengthindex=s.indexOf("length:");
		if(lengthindex!=-1){
			Lengthinfo lengthinfo=new Lengthinfo();
			String numstr=s.substring("length:".length(), s.indexOf(";"));
			if(numstr.indexOf(">=")!=-1){
				lengthinfo.setIfinfo(">=");
				lengthinfo.setLength(Integer.parseInt(numstr.substring(2, numstr.length())));
			}else if(numstr.indexOf("<=")!=-1){
				lengthinfo.setIfinfo("<=");
				lengthinfo.setLength(Integer.parseInt(numstr.substring(2, numstr.length())));
			}else if(numstr.indexOf(">")!=-1){
				lengthinfo.setIfinfo(">");
				lengthinfo.setLength(Integer.parseInt(numstr.substring(1, numstr.length())));
			}else if(numstr.indexOf("<")!=-1){
				lengthinfo.setIfinfo("<");
				lengthinfo.setLength(Integer.parseInt(numstr.substring(1, numstr.length())));
			}
			String cleandel=s.substring(s.indexOf(";")+1,s.length());
			String []deletearray=cleandel.split("◎");
			lengthinfo.setIsDetele(deletearray[1]);
			
			String []cleanarray=deletearray[0].split("clean:");
			if(cleanarray.length>0){
				lengthinfo.setCleanList(new ArrayList<String>());
				for(String clean:cleanarray){
					if(!clean.equals("")){
						lengthinfo.getCleanList().add(clean);
					}
				}
			}
			return lengthinfo;
		}
		return null;
	}
	
}
